package com.kingbi.system.login.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.beanutils.BasicDynaBean;

import com.kingbi.common.MD5;
import com.kingbi.system.function.jsfBean.FunctionDataBean;
import com.kingbi.system.function.model.FunctionModel;
import com.kingbi.system.login.dao.LoginDao;
import com.kingbi.system.login.model.LoginModel;
import com.kingbi.util.ConstantFiles.SysMagContant;
public class LoginServiceImpl implements LoginService {

	private LoginDao loginDao;

	public LoginDao getLoginDao() {
		return loginDao;
	}

	public void setLoginDao(LoginDao loginDao) {
		this.loginDao = loginDao;
	}

	/**
	 * 校验当前登录用户
	 * 1、用户名密码必须正确
	 * 2、必须具有一个角色和一个权限
	 */
	public LoginModel verifyLoginUser(String userID, String pwd) throws SQLException {
		LoginModel loginModel = null;
		StringBuilder sb = new StringBuilder();
		sb.append(" select ");
		sb.append(" pu.user_id,pu.user_name,pu.phone,pu.mobile,pu.email,pu.remark,");
		sb.append(" count(distinct pur.role_code) role_code_count,count(distinct prf.function_code) func_code_count");
		sb.append(" from p_user pu");
		sb.append(" left join p_user_role pur on pu.user_id=pur.user_id");
		sb.append(" left join p_role_function prf on prf.role_code=pur.role_code");
		sb.append(" where pu.user_id=? and pu.password=? and pu.valid=1");
		sb.append(" group by pu.user_id,pu.user_name,pu.phone,pu.mobile,pu.email,pu.remark");
		try {
			List<Object> listUser = loginDao.findBySQL(sb.toString(),
					new Object[] { userID, new MD5().getMD5ofStr(pwd) });
			if (listUser != null && listUser.size()-1==0 ) {
				loginModel = new LoginModel();
				BasicDynaBean bean = (BasicDynaBean) listUser.get(0);
				loginModel.setUserID( bean.get("user_id") !=null ? bean.get("user_id").toString():"");
				loginModel.setUserName(bean.get("user_name") !=null? bean.get("user_name").toString():"");
				loginModel.setPhone(bean.get("phone") != null ? bean.get("phone").toString() : "");
				loginModel.setMobile(bean.get("mobile") != null ? bean.get("mobile").toString() : "");
				loginModel.setEmail(bean.get("email") != null ? bean.get("email").toString() : "");
				loginModel.setRemark(bean.get("remark") != null ? bean.get("remark").toString() : "");
				loginModel.setRoleCount(bean.get("role_code_count") != null ? bean.get("role_code_count").toString() : "0");
				loginModel.setFuncCount(bean.get("func_code_count") != null ? bean.get("func_code_count").toString() : "0");
			}
		} catch (SQLException e) {
			throw e;
		}
		return loginModel;
	}
	

	/*
	 * 查询顶部菜单
	 */
	public List<FunctionDataBean> loadHeadMenu(String userID)
			throws SQLException,Exception {
		StringBuffer sqlBuf = new StringBuffer();
		sqlBuf.append(" select ");
		sqlBuf.append(" pu.user_id,pf.func_code,pf.name,pf.page_url,pf.father_fun_code,pf.haschild,pf.func_level,");
		sqlBuf.append(" max(prf.role_right) role_right");
		sqlBuf.append(" from p_user pu");
		sqlBuf.append(" left join p_user_role pur on pu.user_id=pur.user_id");
		sqlBuf.append(" left join p_role_function prf on prf.role_code=pur.role_code");
		sqlBuf.append(" left join p_function pf on pf.func_code=prf.function_code ");
		sqlBuf.append(" where pu.user_id=? and pu.valid=1 and pf.fun_status=1");
		sqlBuf.append(" and pf.func_level='"+SysMagContant.FUNCTION_LEVEL_ONE + "'");
		sqlBuf.append(" group by pu.user_id,pf.func_code,pf.name,pf.page_url,pf.father_fun_code,pf.haschild,pf.func_level");
		sqlBuf.append(" order by pf.fun_order asc");
		
		List<Object> list_bean = null;
		List<FunctionDataBean> funList = new ArrayList<FunctionDataBean>();

		try {
			list_bean = loginDao.findBySQL(sqlBuf.toString(),new Object[] { userID });
		} catch (SQLException e) {
			throw new SQLException("" + e);
		}

		if (list_bean != null && list_bean.size() > 0) {
			Iterator<Object> beanItera = list_bean.iterator();
			while (beanItera.hasNext()) {
				BasicDynaBean obj_bean = (BasicDynaBean) beanItera.next();
				FunctionDataBean funModel = new FunctionDataBean();
				funModel.setFuncCode(obj_bean.get("func_code")!=null?obj_bean.get("func_code").toString():"");
				funModel.setFunName(obj_bean.get("name")!=null?obj_bean.get("name").toString():"");
				funModel.setFunRight(obj_bean.get("role_right")!=null?obj_bean.get("role_right").toString():"");
				funModel.setPageUrl(obj_bean.get("page_url")!=null?obj_bean.get("page_url").toString():"");
				funModel.setHasChild(obj_bean.get("haschild")!=null?obj_bean.get("haschild").toString():"");
				funModel.setFatherFunCode(obj_bean.get("father_fun_code")!=null?obj_bean.get("father_fun_code").toString():"");
				funModel.setFuncLevel(obj_bean.get("func_level")!=null?obj_bean.get("func_level").toString():"");

				funList.add(funModel);
			}
		}
		return funList;
	}

	/**
	 * 查询左侧菜单
	 */
	public List<FunctionDataBean> loadLeftMenu(String userID, String funCode,
			String funLevel) throws SQLException,Exception {
		StringBuffer sqlBuf = new StringBuffer();
		sqlBuf.append(" select ");
		sqlBuf.append(" pu.user_id,pf.func_code,pf.name,pf.page_url,pf.father_fun_code,pf.haschild,pf.func_level,");
		sqlBuf.append(" max(prf.role_right) role_right");
		sqlBuf.append(" from p_user pu");
		sqlBuf.append(" left join p_user_role pur on pu.user_id=pur.user_id");
		sqlBuf.append(" left join p_role_function prf on prf.role_code=pur.role_code");
		sqlBuf.append(" left join p_function pf on pf.func_code=prf.function_code ");
		sqlBuf.append(" where pu.user_id=? and pu.valid=1 and pf.fun_status=1");
		sqlBuf.append(" and pf.father_fun_code=? and pf.func_level=?");
		sqlBuf.append(" group by pu.user_id,pf.func_code,pf.name,pf.page_url,pf.father_fun_code,pf.haschild,pf.func_level");
		sqlBuf.append(" order by pf.fun_order asc");

		List<Object> list_bean = null;
		List<FunctionDataBean> funList = new ArrayList<FunctionDataBean>();

		try {
			list_bean = loginDao.findBySQL(sqlBuf.toString(), new Object[] {
					userID, funCode, funLevel });
		} catch (SQLException e) {
			throw new SQLException("" + e);
		}

		if (list_bean != null && list_bean.size() > 0) {
			Iterator<Object> beanItera = list_bean.iterator();
			while (beanItera.hasNext()) {
				BasicDynaBean obj_bean = (BasicDynaBean) beanItera.next();
				FunctionDataBean funModel = new FunctionDataBean();
				funModel.setFuncCode(obj_bean.get("func_code")!=null?obj_bean.get("func_code").toString():"");
				funModel.setFunName(obj_bean.get("name")!=null?obj_bean.get("name").toString():"");
				funModel.setFunRight(obj_bean.get("role_right")!=null?obj_bean.get("role_right").toString():"");
				funModel.setPageUrl(obj_bean.get("page_url")!=null?obj_bean.get("page_url").toString():"");
				funModel.setHasChild(obj_bean.get("haschild")!=null?obj_bean.get("haschild").toString():"");
				funModel.setFatherFunCode(obj_bean.get("father_fun_code")!=null?obj_bean.get("father_fun_code").toString():"");
				funModel.setFuncLevel(obj_bean.get("func_level")!=null?obj_bean.get("func_level").toString():"");

				funList.add(funModel);
			}
		}
		return funList;
	}
	
}
